<?php
/**
 * class App_Controller_Abstract
 *
 * @author kim
 */
class App_Controller_Abstract extends Zend_Controller_Action
{
    /**
     * redirect to /user/login if user is not logged in
     */
    protected function _redirectIfNotLoggedIn()
    {
        if (false == App_Model_User::isLoggedIn()) {
            $this->_helper->redirector('login', 'user');
        }
    }

    /**
     * redirect to /index/index if logged in user is not viewing his own
     */
    protected function _redirectIfNotViewingOwn($key = 'id')
    {
        // redirect if not logged in
        $this->_redirectIfNotLoggedIn();

        // get logged in user id
        $myUserId = App_Model_User::getStoredMyUserId();

        // check if current logged in user is viewing his/her own
        $userId = $this->getRequest()->getParam($key);
        if ($userId and ($userId != $myUserId)) {
            $this->_helper->redirector('index', 'index');
        }

        return $myUserId;
    }

    /**
     * redirect to /index/index if the user is blocked
     */
    protected function _redirectIfBlocked($uid)
    {
        $this->_redirectIfNotLoggedIn();
        if ($uid) {
            if ($myUserId = App_Model_User::getStoredMyUserId()) {
                $userModel = new App_Model_User();
                if ($userModel->isBlocked($myUserId, $uid)) {
                    $this->_helper->redirector('index', 'index');
                }
            }
        }
    }

}
